import { apiService } from './api'

export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  user: {
    id: string
    username: string
    name: string
    role: string
    avatar?: string
  }
}

export interface User {
  id: string
  username: string
  name: string
  role: string
  avatar?: string
  email?: string
  phone?: string
}

export const authService = {
  // 登录
  async login(credentials: LoginRequest): Promise<LoginResponse> {
    return apiService.post<LoginResponse>('/api/auth/login', credentials)
  },

  // 登出
  async logout(): Promise<void> {
    return apiService.post<void>('/api/auth/logout')
  },

  // 获取当前用户信息
  async getCurrentUser(): Promise<User> {
    return apiService.get<User>('/api/auth/me')
  },

  // 刷新token
  async refreshToken(): Promise<{ token: string }> {
    return apiService.post<{ token: string }>('/api/auth/refresh')
  },

  // 修改密码
  async changePassword(oldPassword: string, newPassword: string): Promise<void> {
    return apiService.post<void>('/api/auth/change-password', {
      oldPassword,
      newPassword,
    })
  },
}